﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class FurnitureOutputSurvivorAdd : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Furniture_FurnitureOutputSurvivorAdd;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<FurnitureOutputSurvivorAddReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}

			if (req.furnitureId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：furnitureId参数为空）" });
				return;
			}
			if (req.survivorId <= 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：survivorId <= 0）" });
				return;
			}

			var furniture = session.minecraftSessionInfo.furnitureOutputList.FirstOrDefault(m => m.FurnitureId == req.furnitureId);
			if (furniture == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：furniture信息不存在）" });
				return;
			}
			if (furniture.Output_SurvivorId > 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：furniture的幸存工作者的id大于0，可能是因为在解除绑定时服务器没有将幸存者id归0）" });
				return;
			}
			var survivor = session.minecraftSessionInfo.survivorList.FirstOrDefault(m => m.SurvivorId == req.survivorId);
			if (survivor == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：survivor == null）" });
				return;
			}
			if (survivor.enumSurvivorState == EnumSurvivorState.WorkForFurnitureOutput)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：id为【{survivor.SurvivorId}】的enumSurvivorState为【{survivor.enumSurvivorState.ToString()}】）" });
				return;
			}

			survivor.State = (int)EnumSurvivorState.WorkForFurnitureOutput;
			furniture.Output_SurvivorId = survivor.SurvivorId;

			var resp = new FurnitureOutputSurvivorAddResp
			{
				furnitureId = furniture.FurnitureId,
				survivorId = survivor.SurvivorId
			};
			session.Send(defCommand, req, resp);
		}
	}
}
